<?php
namespace app\wxpro\controller;
use think\Db;
use think\request;
use think\Controller;
use app\service\orderService;
use app\service\chargeRuleService;
use app\service\wechatService;
use app\wxpro\controller\Wxmsg;
class Bindcar extends Controller{
	//小区临时车列表
	public function temporaryCarList(){
		$openid=request()->get("openid");
		$carList=Db::table("car_temporary")->where("openid",$openid)->select();
	}
	//车辆列表
	public function carlist(){
		$openid=request()->get("openid");
		$carList=Db::table("car_wx_bind")->where("openid",$openid)->select();
		$arr["status"]="1";
		$arr["carList"]=$carList;
		return json_encode($arr);
	}
	
	//查询车辆是否为月租地库信息
	public function carinfo(){
		$car_number=request()->get("car_number");
		//$carinfo=(new wechatService())->numberExemption($car_number);
		
		$carinfo=Db::table('package')->where("car_number",$car_number)->find();
		if(empty($carinfo)){
			$arr["status"]="0";
			$arr["error"]="该车辆尚未办理月卡";
			return json_encode($arr);
		}else{
			$arr["status"]="1";
			$parkinglotinfo=(new orderService())->parkingInfo($carinfo["out_parking_id"]);
			$carinfo["car_renewal_period"]=$parkinglotinfo["car_renewal_period"];
			$carinfo["money"]=$carinfo["car_renewal_period"]*$carinfo["price"];
			$carinfo["parking_name"]=$parkinglotinfo["parking_name"];
			$arr["carinfo"]=$carinfo;
			return json_encode($arr);
		}
	}
	
	//车辆绑定
	public function caradd(){
		$data["car_number"]=request()->get("car_number");
		$data["openid"]=request()->get("openid");
		$res=Db::table('car_wx_bind')->where("openid",$data["openid"])->where("car_number",$data["car_number"])->find();
		if($res){
			$arr["status"]="0";
			$arr["error"]="您已经绑定过改车牌";
			return json_encode($arr);
		}else{
			$re=Db::table('car_wx_bind')->insert($data);
			if($re){
				$arr["status"]="1";
				$arr["error"]="车牌绑定成功";
				$ctime=time()-518400;
				$wxproinfo=Db::table("wxpro_form")->where("openid",$data["openid"])->where("ctime",'>',$ctime)->find();
				$bindtime=date('Y-m-d H:i:s');
				if(!empty($wxproinfo)){
					$res=(new Wxmsg())->bindcar($wxproinfo["openid"],$wxproinfo["form_id"],$data["car_number"],$bindtime);
					$re=json_decode($res,1);
					if($re["errcode"]=="0"){
						if($wxproinfo["num"]=="1"){
							Db::table("wxpro_form")->where("id",$wxproinfo["id"])->delete();
						}else{
							Db::table("wxpro_form")->where("id",$wxproinfo["id"])->update(["num"=>($wxproinfo["num"]-1)]);
						}
					}else{
						Db::table("wxpro_form")->where("id",$wxproinfo["id"])->delete();
					}
					//var_dump($re);	
				}
				return json_encode($arr);
			}else{
				$arr["status"]="0";
				$arr["error"]="车牌绑定失败";
				return json_encode($arr);
			}
		}
	}
	
	//车辆解绑
	public function cardel(){
		$car_number=request()->get("car_number");
		$openid=request()->get("openid");
		$res=Db::table('car_wx_bind')->where("openid",$openid)->where("car_number",$car_number)->delete();
		if($res){
			$arr["status"]="1";
			$arr["success"]="车辆删除成功";
			return json_encode($arr);
		}else{
			$arr["status"]="0";
			$arr["error"]="车辆删除失败";
			return json_encode($arr);
		}
	}
	
	public function BindPackageList(){
		$car_number=request()->get("car_number");
		$packageList=Db::table("package")->where("car_number",$car_number)->select();
		foreach($packageList as $key=>$values){
			$parkinglotinfo=Db::table("parkinglotinfo")->where("out_parking_id",$values["out_parking_id"])->find();
			$packageList[$key]["parking_name"]=$parkinglotinfo["parking_name"];
		}
		if(empty($packageList)){
			$arr["status"]="0";
			$arr["error"]="暂无车辆信息";
			return json_encode($arr);
		}else{
			$arr["status"]="1";
			$arr["packageList"]=$packageList;
			return json_encode($arr);
		}
	}
	
	
	
   
    
    
}